#ifndef _LINK_QUEUE_H_
#define _LINK_QUEUE_H_

#include "Queue.h"
#include "DualLinkList.h"
#include "Exception.h"

namespace MySTL
{

template < typename T >
class LinkQueue : public Queue<T>
{
public:
	~LinkQueue();

	bool enqueue(const T& value);
	T dequeue();
	T& top();
	const T& top() const;
	void clear();
	int length() const;

protected:
	DualLinkList<T> m_list;
};

template < typename T >
LinkQueue<T>::~LinkQueue()
{
	m_list.clear();
}

template < typename T >
bool LinkQueue<T>::enqueue(const T& value)
{
	return m_list.insert(m_list.length(), value);
}

template < typename T >
T LinkQueue<T>::dequeue()
{
	if (m_list.length() <= 0)
	{
		THROW_EXCEPTION(InvalidOperationException, "Invalid operate in T LinkQueue<T>::pop()");
	}

	T ret = m_list[0];

	m_list.remove(0);

	return ret;
}

template < typename T >
T& LinkQueue<T>::top()
{
	if (m_list.length() <= 0)
	{
		THROW_EXCEPTION(InvalidOperationException, "Invalid operate in T& LinkQueue<T>::top()");
	}

	return m_list[0];
}

template < typename T >
const T& LinkQueue<T>::top() const
{
	return const_cast<LinkQueue<T>*>(this)->top();
}

template < typename T >
void LinkQueue<T>::clear()
{
	m_list.clear();
}

template < typename T >
int LinkQueue<T>::length() const
{
	return m_list.length();
}

}

#endif